<template xlang="wxml">
	<view class="tki-tree" style="height: 100% !important;">
		<!-- <view class="tki-tree-mask" :class="{'show':showTree}" @tap="_cancel"></view>
		<view class="tki-tree-cnt" :class="{'show':showTree}"> -->
		<!-- <view class="tki-tree-bar">
				<view class="tki-tree-bar-cancel" :style="{'color':cancelColor}" hover-class="hover-c" @tap="_cancel">取消</view>
				<view class="tki-tree-bar-title" :style="{'color':titleColor}">{{title}}</view>
				<view class="tki-tree-bar-confirm" :style="{'color':confirmColor}" hover-class="hover-c" @tap="_confirm">确定</view>
			</view> -->
		<view class="tki-tree-view" style="height: 100%;top: 0;padding-top: 0">
			<scroll-view class="tki-tree-view-sc" :scroll-y="true">
				<block v-for="(item, index) in treeList" :key="index">
					<view class="tki-tree-item" :style="[{
							paddingLeft: item.rank*15 + 'px',
							zIndex: item.rank*-1 +50
						}]" :class="{
							border: border === true,
							show: item.show,
							last: item.lastRank,
							showchild: item.showChild,
							open: item.open,
						}">
						<!-- @tap.stop="_treeItemTap(item, index)"  -->

						<view class="tki-tree-label">
							<image class="tki-tree-icon"
								:src="item.lastRank ? lastIcon : item.showChild ? currentIcon : defaultIcon"></image>
							{{item.name}}
						</view>
						<view class="tki-tree-check" @tap.stop="_treeItemSelect(item, index)"
							v-if="personselectParent?true:item.lastRank">
							<view class="tki-tree-check-yes" v-if="item.checked" :class="{'radio':!personmultiple}"
								:style="{'border-color':confirmColor}">
								<view class="tki-tree-check-yes-b" :style="{'background-color':confirmColor}"></view>
							</view>
							<view class="tki-tree-check-no" v-else :class="{'radio':!personmultiple}"
								:style="{'border-color':confirmColor}"></view>
						</view>

					</view>
				</block>
			</scroll-view>
		</view>
	</view>
	</view>
</template>

<script>
	export default {
		name: "tki-tree",
		props: {
			personRange: {
				type: Array,
				default: function() {
					return []
				}
			},
			cd: {
				type: Array,
				default: function() {
					return []
				}
			},
			falgNum: {
				type: String,
				default: '0'
			},
			// personAll: {
			// 	type: Object,
			// 	default: function() {
			// 		return {}
			// 	}
			// },
			// 
			checked: {
				type: Array,
				default: function() {
					return []
				}
			},
			type: {
				type: String,
				default: 0,
			},
			personidKey: {
				type: String,
				default: 'id'
			},

			personrangeKey: {
				type: String,
				default: 'label'
			},
			title: {
				type: String,
				default: ''
			},
			personmultiple: { // 是否可以多选
				type: Boolean,
				default: false
				// default: true
			},
			personselectParent: { //是否可以选父级
				type: Boolean,
				default: false
			},
			personfoldAll: { //折叠时关闭所有已经打开的子集，再次打开时需要一级一级打开
				type: Boolean,
				default: false
			},
			confirmColor: { // 确定按钮颜色
				type: String,
				default: '' // #1752db
			},
			cancelColor: { // 取消按钮颜色
				type: String,
				default: '' // #757575
			},
			titleColor: { // 标题颜色
				type: String,
				default: '' // #1752db
			},
			currentIcon: { // 展开时候的ic
				type: String,
				default: ''
			},
			defaultIcon: { // 折叠时候的ic
				type: String,
				default: ''
			},
			lastIcon: { // 没有子集的ic
				type: String,
				default: ''
			},
			border: { // 是否有分割线
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				showTree: false,
				treeList: [],
				selectIndex: -1,
				source: {},
				arr: [],
				typeNum: "0"
			}
		},
		computed: {},
		methods: {
			_show() {
				this.showTree = true
			},
			_hide() {
				this.showTree = false
			},
			_cancel() {
				this._hide()
				this.$emit("cancel", '');
			},
			_confirm() {
				// 处理所选数据
				let rt = [],
					obj = {};
				this.treeList.forEach((v, i) => {
					if (this.treeList[i].checked) {
						obj = {}
						obj.parents = this.treeList[i].parents
						obj = Object.assign(obj, this.treeList[i].source)
						// 移除子元素
						delete obj.children
						rt.push(obj)
					}
				})
				this._hide()
				// console.log(rt)
				this.$emit("confirm", rt);
			},
			//扁平化树结构
			_renderTreeList(list = [], rank = 0, parentId = [], parents = []) {
				list.forEach(item => {
					// console.log(item[this.personrangeKey])
					this.treeList.push({
						id: item[this.personidKey],
						name: item[this.personrangeKey],
						source: item,
						parentId, // 父级id数组
						parents, // 父级id数组
						rank, // 层级
						showChild: false, //子级是否显示
						open: false, //是否打开
						show: rank === 0, // 自身是否显示
						hideArr: [],
						orChecked: item.checked ? item.checked : false,
						checked: item.checked ? item.checked : false,
					})
					if (Array.isArray(item.children) && item.children.length > 0) {
						// console.log(item)
						let parentid = [...parentId],
							parentArr = [...parents],
							childrenid = [...childrenid];
						delete parentArr.children
						parentid.push(item[this.personidKey]);
						parentArr.push({
							[this.personidKey]: item[this.personidKey],
							[this.personrangeKey]: item[this.personrangeKey]
						})
						this._renderTreeList(item.children, rank + 1, parentid, parentArr);
					} else {
						this.treeList[this.treeList.length - 1].lastRank = true;
					}
				})
				// console.log(list)
			},
			// 处理默认选择
			_defaultSelect() {
				// console.log(this.checked)
				for (var i = 0; i < this.treeList.length; i++) {
					this.treeList[i].checked = false
					for (var j = 0; j < this.checked.length; j++) {
						if (this.treeList[i].id == this.checked[j].fId) {
							this.treeList[i].checked = true


						}
					}

				}




			},
			// 点击
			_treeItemTap(item, index) {

				if (item.lastRank === true) {
					//点击最后一级时触发事件
					this.treeList[index].checked = !this.treeList[index].checked
					this._fixMultiple(index)
					return;
				}
				let list = this.treeList;
				let id = item.id;
				item.showChild = !item.showChild;
				item.open = item.showChild ? true : !item.open;
				list.forEach((childItem, i) => {
					if (item.showChild === false) {
						//隐藏所有子级
						if (!childItem.parentId.includes(id)) {
							return;
						}
						if (!this.personfoldAll) {
							if (childItem.lastRank !== true && !childItem.open) {
								childItem.showChild = false;
							}
							// 为隐藏的内容添加一个标记
							if (childItem.show) {
								childItem.hideArr[item.rank] = id
							}
						} else {
							if (childItem.lastRank !== true) {
								childItem.showChild = false;
							}
						}
						childItem.show = false;
					} else {
						// 打开子集
						if (childItem.parentId[childItem.parentId.length - 1] === id) {
							childItem.show = true;
						}
						// 打开被隐藏的子集
						if (childItem.parentId.includes(id) && !this.personfoldAll) {
							// console.log(childItem.hideArr)
							if (childItem.hideArr[item.rank] === id) {
								childItem.show = true;
								if (childItem.open && childItem.showChild) {
									childItem.showChild = true
								} else {
									childItem.showChild = false
								}
								childItem.hideArr[item.rank] = null
							}
							// console.log(childItem.hideArr)
						}
					}
				})
				// console.log(this.treeList)
			},
			_treeItemSelect(item, index) {
				// console.log(this.arr,this.arr.length)
				if (this.typeNum == 0) {
					this.$emit("sonTate", item.source);
				} else if (this.typeNum == 1) {
					if (item.checked) {
						for (var i = 0; i < this.arr.length; i++) {
							if (this.arr[i].fId == item.id) {
								this.arr.splice(i, 1);
							}
						}
					} else {
						this.arr.push(item.source)
					}
					this.$emit("sonTate", this.arr);
				}


				this.treeList[index].checked = !this.treeList[index].checked
				this._fixMultiple(index)
				this._hide()

			},

			// 处理单选多选
			_fixMultiple(index) {
				if (!this.personmultiple) {
					// 如果是单选
					this.treeList.forEach((v, i) => {
						if (i != index) {
							this.treeList[i].checked = false
						} else {
							this.treeList[i].checked = true
						}
					})
				}
			},
			// 重置数据
			_reTreeList() {
				this.treeList.forEach((v, i) => {
					this.treeList[i].checked = v.orChecked
				})
			},
			_initTree(personRange = this.personRange) {

				this.treeList = [];
				this._renderTreeList(personRange);
				this.$nextTick(() => {
					this._defaultSelect(personRange)
				})
			}
		},
		watch: {
			personRange(list) {
				this._initTree(list);
			},
			personmultiple() {
				if (this.personRange.length) {
					this._reTreeList();
				}
			},
			personselectParent() {
				this._initTree();

			},
			checked(data) {
				// console.log(data)




				// for (var i = 0; i < this.treeList.length; i++) {
				// 	this.treeList[i].checked = false
				// 	for (var j = 0; j < data.length; j++) {
				// 		if (this.treeList[i].id == data[j].fId) {
				// 			this.treeList[i].checked = true
				// 			this.arr.push({
				// 				fId: this.treeList[i].id,
				// 				fStaffName: this.treeList[i].name
				// 			})

				// 		}
				// 	}



				// var result = [];
				// var obj = {};
				// for (var i = 0; i < this.arr.length; i++) {
				// 	if (!obj[this.arr[i].fId]) {
				// 		result.push(this.arr[i]);
				// 		obj[this.arr[i].fId] = true;
				// 	}
				// }
				// this.arr=result


			},
			cd(data) {
				// this.arr = data
				// if (this.personRange.length) {
				// 	this._reTreeList();
				// }
				// console.log(this.treeList,data)
			},

			falgNum: {
				handler(e) {
					if (e != 0) {

						this.arr = []
					}

				},
				immediate: true
			},
			type: {
				handler(e) {
					this.typeNum = e
					if (e == 0) {
						// this.Persosnmultiples = false
					} else if (e == 1) {
						// this.Persosnmultiples = true
					}

				},
				immediate: true
			}

		},
		mounted() {
			this._initTree();
		}
	}
</script>

<style scoped>
	@import "./style1.css";
</style>
